package com.oa.service.impl;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oa.bean.Role;
import com.oa.bean.Section;
import com.oa.bean.User;
import com.oa.dao.PersonalDao;
import com.oa.service.PersonalService;

@Service
public class PersonalServiceImpl implements PersonalService {

	@Resource
	PersonalDao personalDao;

	@Override
	public User getUserById(Integer uid) {
		// TODO Auto-generated method stub
		User user = personalDao.selectPersonal(uid);
		return user;
	}

	@Override
	public Map<String, Object> getUserSectionRole(Integer uid) {
		// TODO Auto-generated method stub
		User user =personalDao.selectPersonal(uid);
		List<Role> roleList = personalDao.selectRoleList();
		List<Section>sectionList = personalDao.selectSectionList();
		Map<String,Object>map = new HashMap<>();
		map.put("user", user);
		map.put("roleList", roleList);
		map.put("sectionList", sectionList);
		return map;
	}

	@Override
	public Map<String, Object> getSectionRole() {
		// TODO Auto-generated method stub
		List<Role> roleList = personalDao.selectRoleList();
		List<Section>sectionList = personalDao.selectSectionList();
		Map<String,Object>map = new HashMap<>();
		map.put("roleList", roleList);
		map.put("sectionList", sectionList);
		return map;
	}

	@Override
	public Integer addUser(User user) {
		// TODO Auto-generated method stub
		Integer result = personalDao.insertUserByUser(user);
		return result;
	}

	@Override
	public Map<String, Object> getUserList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		Page page = PageHelper.startPage((int)map.get("currentPage"), 2,"u.uid desc");
		List<User>list = personalDao.selectUserList(map);
		map.put("userList", list);
		map.put("total", page.getPages());
		map.put("limt", 2);
		return map;
	}

	@Override
	public String modifyInfor(User user) {
		// TODO Auto-generated method stub
		Integer result = personalDao.updateUser(user);
		System.out.println(result);
		if(result!=0){
			return "true";
		}
		return "false";
	}

	@Override
	public String delectUserInfo(int uid) {
		// TODO Auto-generated method stub
		Integer result = personalDao.delectUserById(uid);
		System.out.println(result);
		if(result!=0){
			return "true";
		}
		return "false";
	}

	@Override
	public String replaceUserImg(Integer uid, String fileName) {
		// TODO Auto-generated method stub
		Map<String,Object>map = new HashMap<>();
		map.put("uid", uid);
		map.put("uhead", fileName);
		Integer result = personalDao.updateUserImg(map);
		if(result!=0){
			return "true";
		}
		return "false";
	}

	@Override
	public String changePassWord(User user) {
		// TODO Auto-generated method stub
		Integer result = personalDao.updatePassWord(user);
		if(result!=0){
			return "true";
		}
		return "false";
	}

	@Override
	public User getUserByName(String userName) {
		// TODO Auto-generated method stub
		User user = personalDao.selectUserByName(userName);
		return user;
	}

	@Override
	public Set<String> getRole(String userName) {
		// TODO Auto-generated method stub
		Integer roles = personalDao.selectRoleByName(userName);
		Set<String> set = new HashSet<>();
		set.add(roles.toString());
		return set;
	}

	@Override
	public String changeOtherPass(User user) {
		// TODO Auto-generated method stub
		Integer result = personalDao.updateOtherPass(user);
		if(result!=0){
			return "true";
		}
		return "false";
	}	
	
}
